package com.wavecoler.professor.servlets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jettison.json.JSONObject;

import com.google.gson.Gson;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.wavecoler.professor.objects.User;

@SuppressWarnings("serial")
public class ModifyUser extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String username = req.getParameter("username");
		Client service = Client.create();
		WebResource webResource = service.resource("http://wavecoler.lag.itesm.mx:8080/WaveColerWebServices/rest/users/user/"+username+"/");
		ClientResponse respuesta = webResource.accept("application/json").get(ClientResponse.class);
		final JSONObject jsonOb = respuesta.getEntity(JSONObject.class);
		
		Gson gson = new Gson();
		BufferedReader br = new BufferedReader(new StringReader(jsonOb.toString()));
		User usuario = gson.fromJson(jsonOb.toString(), User.class);
		
			HttpSession session = req.getSession();
			session.setAttribute("user",usuario);
		String address = "jsps/ModifyUser.jsp";
	    RequestDispatcher dispatcher = req.getRequestDispatcher(address);
	    dispatcher.forward(req, resp);
	}
	
	

}
